package com.jnsw.testkotlin.tab;


import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.jnsw.testkotlin.R;
import com.jnsw.testkotlin.bean.RxBus;
import com.jnsw.testkotlin.bean.User;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action1;

/**
 * A simple {@link Fragment} subclass.
 */
public class Tab1Fragment extends Fragment {

    Observable<User> ob ;
    Subscription subscription;
    View view;
    public Tab1Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //创建被观察者
        ob = RxBus.getInstance().toObserverable(User.class) ;
        //订阅观察事件
        subscription =     ob.subscribe(new Action1<User>() {
            @Override
            public void call(User s) {
                System.out.println( "ffffffffffffffff---------------------------------- " + s.getName()  );
            }
        }) ;
        User user = new User();
        user.setName("userssssss");
        user.setAge(36);
        //发送内容
        RxBus.getInstance().post( user);

        view = inflater.inflate(R.layout.fragment_tab1, container, false);


        Button wb = (Button) view.findViewById(R.id.wb);
        wb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent =  getActivity().getPackageManager().getLaunchIntentForPackage("com.sina.weibo");
                if (intent!= null){

                    getActivity().startActivity(intent);
                }else {
                    Toast.makeText(getActivity(),"未安装微博",Toast.LENGTH_SHORT).show();
                }

            }
        });
        Button wx = (Button) view.findViewById(R.id.wx);
        wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent =  getActivity().getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                if (intent!= null){

                    getActivity().startActivity(intent);
                }else {
                    Toast.makeText(getActivity(),"未安装微信",Toast.LENGTH_SHORT).show();
                }
            }
        });
        Button qq = (Button) view.findViewById(R.id.qq);
        qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent =  getActivity().getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq");
                if (intent!= null){

                    getActivity().startActivity(intent);
                }else {
                    Toast.makeText(getActivity(),"未安装QQ",Toast.LENGTH_SHORT).show();
                }
            }
        });
        return view;


    }

    @Override
    public void onDestroyView() {

        if (subscription != null){
            subscription.unsubscribe();
        }
        super.onDestroyView();
    }
}
